package com.dinggou.shopjb.activitys;

import static com.dinggou.shopjb.constants.Constant.H5_LEVEL_RULE;
import static com.dinggou.shopjb.utils.RouteUtil.gotoActivity;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.net.http.SslError;
import android.os.Build;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinggou.shopjb.R;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.UserInfoBean;
import com.dinggou.shopjb.utils.DataUtils;

public class VIPCenterActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "VIPCenterActivity";
    private ProgressBar pb_experience;
    private RelativeLayout vip_center_header;
    private WebView wv_detail;
    private int [] level_1 = new int[]{Color.rgb(195, 218, 229), Color.rgb(122, 143, 153)};
    private int [] level_2 = new int[]{Color.rgb(128, 181, 213), Color.rgb(77, 128, 164) };
    private int [] level_3 = new int[]{Color.rgb(75, 112, 184), Color.rgb(121, 154, 223) };
    private int [] level_4 = new int[]{Color.rgb(242, 179, 117), Color.rgb(209, 147, 86) };
    private int [] level_5 = new int[]{Color.rgb(235, 159, 130), Color.rgb(192, 113, 82) };
    private int [] level_6 = new int[]{Color.rgb(89, 51, 128), Color.rgb(36, 20, 51) };
    private int [] level_7 = new int[]{Color.rgb(63, 63, 63), Color.rgb(34, 34, 34) };
    private TextView name_tv,tv_experience_desc,tv_experience_log;
    private ImageView icon_level,iv_vip_back;
    private int[] levels = new int[]{R.drawable.img_level_up_1,R.drawable.img_level_up_2, R.drawable.img_level_up_3, R.drawable.img_level_up_4,
            R.drawable.img_level_up_5, R.drawable.img_level_up_6,R.drawable.img_level_up_7};
    @Override
    protected int setContentView() {
        return R.layout.activity_vip_center;
    }

    @Override
    protected void findViews() {

        vip_center_header = $(R.id.vip_center_header);
        wv_detail = $(R.id.wv_detail);
        pb_experience = $(R.id.pb_experience);
        name_tv = $(R.id.name_tv);
        tv_experience_desc = $(R.id.tv_experience_desc);
        tv_experience_log = $(R.id.tv_experience_log);
        icon_level = $(R.id.icon_level);
        iv_vip_back = $(R.id.iv_vip_back);
        setViewDatas();
        WebSettings webSettings = wv_detail.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        wv_detail.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        wv_detail.loadUrl(H5_LEVEL_RULE);

    }

    @Override
    protected void setListeners() {
        iv_vip_back.setOnClickListener(this);
        tv_experience_log.setOnClickListener(this);
    }

    private void setViewDatas(){
        UserInfoBean bean = DataUtils.getUserInfo(mContext);
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
        if (bean.level ==1){
            gradientDrawable.setColors(level_1);
            name_tv.setText("恭喜您!\r\n已经获得大众会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_1);

        }else if (bean.level ==2){
            gradientDrawable.setColors(level_2);
            name_tv.setText("恭喜您!\r\n已经获得青铜会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_2);

        } else if (bean.level ==3) {
            gradientDrawable.setColors(level_3);
            name_tv.setText("恭喜您!\r\n已经获得白银会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_3);
        }else if (bean.level ==4) {
            gradientDrawable.setColors(level_4);
            name_tv.setText("恭喜您!\r\n已经获得黄金会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_4);
        }else if (bean.level ==5) {
            gradientDrawable.setColors(level_5);
            name_tv.setText("恭喜您!\r\n已经获得铂金会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_5);
        }else if (bean.level ==6) {
            gradientDrawable.setColors(level_6);
            name_tv.setText("恭喜您!\r\n已经获得钻石会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_6);
        }else if (bean.level ==7) {
            gradientDrawable.setColors(level_7);
            name_tv.setText("恭喜您!\r\n已经获得黑钻会员特权");
            icon_level.setImageResource(R.drawable.img_level_up_7);
        }
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        vip_center_header.setBackground(gradientDrawable);
        tv_experience_desc.setText("经验值:"+bean.experience+"/"+bean.level_max);
        double value = (double) (bean.experience - bean.level_min) / (double)(bean.level_max - bean.level_min);
        int progrss =  (int) (value*100);
        if (progrss > 100){
            pb_experience.setProgress(100);
        }else{
            pb_experience.setProgress(progrss);
        }

    }
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.tv_experience_log){
            gotoActivity(mContext, EXPRecordActivity.class, null);
            return;
        }
        if(view.getId() == R.id.iv_vip_back){
            finish();
            return;
        }
    }
}
